﻿// <copyright file="CameraManager.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

public class CameraManager : UnityEngine.MonoBehaviour, ICameraService
{
    public float StartingCameraSize = 12;
    public UnityEngine.Transform BackgroundQuad;
    private UnityEngine.Camera[] cameras;

    private float currentSize = 0;

    public float CurrentSize
    {
        get
        {
            return this.currentSize;
        }
    }

    public float AspectRatio
    {
        get
        {
            return this.cameras[0].aspect;
        }
    }

    public UnityEngine.Camera Camera
    {
        get
        {
            return this.cameras[0];
        }
    }

    public void SetOrthograhicSize(float size)
    {
        if (this.currentSize == size)
        {
            return;
        }

        for (int index = 0; index < this.cameras.Length; ++index)
        {
            this.cameras[index].orthographicSize = size;
        }

		UnityEngine.Camera.main.orthographicSize = size;
        
        // Now, change the background size to match the whole camera specter.
        if (this.BackgroundQuad != null)
        {
            // Compute the size of the orthographic...
            float screenSizeX = size * this.cameras[0].aspect;
            float screenSizeY = size;

            this.BackgroundQuad.transform.localScale = new UnityEngine.Vector3(screenSizeX * 2, screenSizeY * 2, 1);
        }

        this.currentSize = size;
    }

    protected virtual System.Collections.IEnumerator Start()
    {
        UnityEngine.Debug.Log("CameraManager start.");

        // Wait for game service!
        IGameService gameService = Services.GetService<IGameService>();
        while (gameService == null)
        {
            yield return null;
            gameService = Services.GetService<IGameService>();
        }

        Services.Register<ICameraService>(this);

        this.cameras = this.GetComponentsInChildren<UnityEngine.Camera>();

        this.SetOrthograhicSize(this.StartingCameraSize);
    }
}